<template>
    <div class="input-container">
      <el-form ref="form" :model="fromData" :label-position="labelPosition" label-width="auto" class="container form-container mx-auto my-auto abc border shadow-sm" style="width: 40%" :rules="rules">
        <h2 class="text-center pb-2">欢迎登录</h2>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="fromData.userName" placeholder="请输入用户名" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="fromData.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item class="row">
          <div class="col btnBox">
            <el-button type="primary" @click="formBtn">登录</el-button>
          </div>
        </el-form-item>
        <div>用户名：admin 密码：123456</div>
      </el-form>
    </div>
</template>


<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import { ElMessage } from 'element-plus'
export default defineComponent({
  name: 'Loagin',
  setup () {
    const store = useStore()
    const router = useRouter()
    const labelPosition = ref('top')
    const fromData = reactive({
      userName: '',
      password: ''
    })
    const rules = {
      userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
      ]
    }
    const form = ref('') as any
    const formBtn = () => {
      form.value.validate((valid : boolean) => {
        if (!valid) {
          ElMessage({
            message: '验证失败请核对后再输入',
            type: 'error',
            showClose: true
          })
        } else {
          store.dispatch('postUserData', fromData).then((res) => {
            if (res.data.data.state) {
              localStorage.setItem('token', res.data.data.token)
              router.push('/')
            }
          })
        }
      })
    }

    return { fromData, labelPosition, rules, form, formBtn }
  }
})
</script>

<style lang="scss" scoped>
.input-container {
  margin-top: 200px;
  .form-container {
    padding: 40px;
  }
  .btnBox {
    display: flex;
    justify-content: flex-end;
  }
}

</style>